package com.ruoyi.repayment.service;

import com.ruoyi.repayment.pojo.*;
import com.ruoyi.repayment.mapper.BenjinMapper;
import com.ruoyi.repayment.mapper.LoanMapper;
import com.ruoyi.repayment.mapper.SuchratesMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

@Service
public class LoanService {
    @Autowired
    LoanMapper loanMapper;

    @Autowired
    SuchratesMapper suchratesMapper;

    @Autowired
    BenjinMapper benjinMapper;
    private LoanService userMapper;


    public List<LoanApplyfor> lists() {
        List<LoanApplyfor> list=loanMapper.findByLists();
        return list;
    }

    public User loginUser(Long userId) {
        User u=loanMapper.loginUser(userId);
        return u;
    }

    public double normal(Long id, Long produecId, Integer userId) {
        //产品
        Product productId = loanMapper.findProductId(produecId);
//        //产品的贷款金额
//        Product productTotal = loanMapper.findProductId(produecId);
        Loan loanId = loanMapper.findLoanId(produecId, userId);
        if (loanId.getRepayment()==1){
            suchrates suchrates = suchratesMapper.findsuchratesId(id);
            //剩余本金
            double figure = suchrates.getFigure();
            //提前还款违约金
            double penalSum = productId.getPenalSum();
            //月供
            double ments = suchrates.getMents();
            double Tqiantotal=ments+figure+figure*penalSum;
            return Tqiantotal;
        }
        if (loanId.getRepayment()==2){
            Benjin benjin = benjinMapper.findsuchratesId(id);
            //剩余本金
            double figure = benjin.getFigure();
            //提前还款违约金
            double penalSum = productId.getPenalSum();
            //月供
            double ments = benjin.getMents();
            double Tqiantotal=ments+figure+figure*penalSum;
            return Tqiantotal;
        }
        return 0.00;
    }

    public double prepayment(Long id, Long produecId, Integer userId) {
        //产品
        Product productId = loanMapper.findProductId(produecId);
        Loan loanId = loanMapper.findLoanId(produecId, userId);
        suchrates suchrates = suchratesMapper.findsuchratesId(id);
        Date date = new Date();
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(date);
//      rightNow.add(Calendar.YEAR,-1);//日期减1年
//      rightNow.add(Calendar.MONTH,3);//日期加3个月
//      rightNow.add(Calendar.DAY_OF_YEAR,5);//日期加5天
        rightNow.add(Calendar.DAY_OF_YEAR,1);
        Date dt1=rightNow.getTime();
        if (loanId.getRepayment()==1){
            suchrates suchsrates = suchratesMapper.findsuchratesId(id);
            //剩余本金
            double figure = suchsrates.getFigure();
            //逾期还款违约金
            double penalSum = productId.getOverdue();
            //月供
            double ments = suchsrates.getMents();
            double Tqiantotal=ments+figure+figure*penalSum*1;
            return Tqiantotal;
        }
        if (loanId.getRepayment()==2){
            Benjin benjin = benjinMapper.findsuchratesId(id);
            //剩余本金
            double figure = benjin.getFigure();
            //逾期还款违约金
            double penalSum = productId.getOverdue();
            //月供
            double ments = benjin.getMents();
            double Tqiantotal=ments+figure+figure*penalSum*1;
            return Tqiantotal;
        }
        return 0.00;
    }

    public User login(User user) {

        User u = userMapper.login(user);
        return u;
    }

    public User register(User user) {
        User u = userMapper.register(user);
        return u;
    }

    public List<Loan> loanList() {
        List<Loan> list = loanMapper.loanList();
        return list;
    }
}
